package com.example.meituan.activity.bean;

import android.database.Cursor;

import java.util.ArrayList;

public class UserBean {
    // 数据库字段名常量，便于维护
    public static final String TABLE_NAME = "d_user";
    public static final String COLUMN_S_ID = "s_id";
    public static final String COLUMN_S_PWD = "s_pwd";
    public static final String COLUMN_S_NAME = "s_name";
    public static final String COLUMN_S_PHONE = "s_phone";
    public static final String COLUMN_S_SHEN = "s_shen";
    public static final String COLUMN_S_IMG = "s_img";

    private String sId;
    private String sPwd;
    private String sName;
    private String sPhone;
    private String sShen;
    private String sImg;

    // 默认构造函数
    public UserBean() {}

    // 带参数的构造函数
    public UserBean(String sId, String sPwd, String sName, String sPhone, String sShen, String sImg) {
        this.sId = sId;
        this.sPwd = sPwd;
        this.sName = sName;
        this.sPhone = sPhone;
        this.sShen = sShen;
        this.sImg = sImg;
    }

    // Getter 和 Setter 方法
    public String getsId() { return sId; }
    public void setsId(String sId) { this.sId = sId; }

    public String getsPwd() { return sPwd; }
    public void setsPwd(String sPwd) { this.sPwd = sPwd; }

    public String getsName() { return sName; }
    public void setsName(String sName) { this.sName = sName; }

    public String getsPhone() { return sPhone; }
    public void setsPhone(String sPhone) { this.sPhone = sPhone; }

    public String getsShen() { return sShen; }
    public void setsShen(String sShen) { this.sShen = sShen; }

    public String getsImg() { return sImg; }
    public void setsImg(String sImg) { this.sImg = sImg; }

    // 将 UserBean 转换为适合插入数据库的参数数组
    public Object[] toInsertParams() {
        return new Object[]{sId, sPwd, sName, sPhone, sShen, sImg};
    }

    // 从 Cursor 中填充 UserBean
    public static UserBean fromCursor(Cursor cursor) {
        if (cursor != null && cursor.moveToFirst()) {
            return new UserBean(
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_S_ID)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_S_PWD)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_S_NAME)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_S_PHONE)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_S_SHEN)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_S_IMG))
            );
        }
        return null;
    }

    // 从 Cursor 集合中批量创建 UserBean 列表
    public static ArrayList<UserBean> fromCursorList(Cursor cursor) {
        ArrayList<UserBean> users = new ArrayList<>();
        if (cursor != null && cursor.moveToFirst()) {
            do {
                users.add(fromCursor(cursor));
            } while (cursor.moveToNext());
        }
        return users;
    }

    @Override
    public String toString() {
        return "UserBean{" +
                "sId='" + sId + '\'' +
                ", sPwd='" + sPwd + '\'' +
                ", sName='" + sName + '\'' +
                ", sPhone='" + sPhone + '\'' +
                ", sShen='" + sShen + '\'' +
                ", sImg='" + sImg + '\'' +
                '}';
    }
}
